//
//  SinglePlayerVController.swift
//  ITOVR
//
//  Created by green on 16/4/24.
//  Copyright © 2016年 jietusoft. All rights reserved.
//

import UIKit
import UtoVRPlayer


let SCREEN_W = CGRectGetWidth(UIScreen.mainScreen().bounds)
let SCREEN_H = CGRectGetHeight(UIScreen.mainScreen().bounds)

// 单独播放页
class SinglePlayerVController: UIViewController, JTPlayerViewDelegate {
    
    @IBOutlet private weak var playView: JTPlayerView!          // 播放界面
    private dynamic var playerItem: UVPlayerItem?               // 视频播放基础信息
    private dynamic var videoId: String?                        // 视频id
    private var constraintPlayViewWHRate: NSLayoutConstraint!
    private var directionMode: PlayerViewControllerDirectionMode = .Default
    
    // MARK: - Life cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setup()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    deinit {
        
        print("\(self) deinit ")
    }
    
    // MARK: - Private
    
    /**
     初始化设置
     */
    private func setup() {
        
        // 设置播放界面约束
        playView.delegate = self
        playView.translatesAutoresizingMaskIntoConstraints = false
        let multiplier = max(SCREEN_H, SCREEN_W) / min(SCREEN_H, SCREEN_W)
        constraintPlayViewWHRate = NSLayoutConstraint(item: playView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: playView, attribute: NSLayoutAttribute.Height, multiplier: multiplier, constant: 0)
        view.addConstraint(constraintPlayViewWHRate)
        
        
        let playerItem  = UVPlayerItem(path: "http://cache.utovr.com/2016012317521234402k/2016012317521234402k.m3u8", type: .Online)
        playView.reset()
        playView.play(playerItem)
        
    }
    
    /**
     取消模态
     */
    private func dismiss() {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //MARK: - Rotation
    
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        
        switch directionMode {
        case .Default:
            
            return UIInterfaceOrientationMask.AllButUpsideDown
        case .HorizontalOnly:
            
            return UIInterfaceOrientationMask.Landscape
        }
    }
    
    // MARK: - JTPlayerViewDelegate
    
    @objc internal func portraitBack(playerView: JTPlayerView) {
        
        dismiss()
    }
    
    @objc internal func horizontalBack(playerView: JTPlayerView) {
        
        if directionMode == .HorizontalOnly {
            
            dismiss()
        }
    }
    
    @objc internal func horizontalBackIsValid() -> Bool {
        
        if directionMode == .HorizontalOnly {
            
            return true
        }
        return false
    }
    
    // MARK: - Public
}
